package com.wali.modules.usr.service;

import com.wali.common.ccp.page.CcpPagination;
import com.wali.common.enums.ErrorCodeEnum;
import com.wali.modules.usr.bo.UsrReceiverBO;
import com.wali.modules.usr.dao.IUsrReceiverDao;
import com.wali.modules.usr.vo.UsrReceiverVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author walixu
 * @ClassName com.wali.modules.usr.service
 * @Description 收件人service类
 * @email wali.xu@kpmg.com
 * @date 22/01/2019 8:13 下午
 */
@Service
public class UsrReceiverService {

    @Autowired
    private IUsrReceiverDao iUsrReceiverDao;

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 查询符合条件的收件人信息
     */
    public CcpPagination<UsrReceiverVO> listUsrReceiver(UsrReceiverBO usrReceiver, int page, int limit, int storeId) {
        UsrReceiverVO usrReceiverVO = UsrReceiverBO.convert(usrReceiver);
        List<UsrReceiverVO> usrReceiverList = iUsrReceiverDao.listUsrReceiver(usrReceiverVO, page, limit, storeId);
        int total = iUsrReceiverDao.countUsrReceiver(usrReceiverVO, storeId);

        CcpPagination<UsrReceiverVO> pagination = new CcpPagination<>();
        pagination.setCode(ErrorCodeEnum.SUCCESS_CODE.getCode());
        pagination.setMsg(ErrorCodeEnum.SUCCESS_CODE.getMessage());
        pagination.setData(usrReceiverList);
        pagination.setCount(total);
        return pagination;
    }

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 删除收件人身份证正面信息
     */
    public int deleteUsrReceiverIdCardFront(UsrReceiverBO usrReceiver) {
        UsrReceiverVO usrReceiverVO = UsrReceiverBO.convert(usrReceiver);

        UsrReceiverVO temp = iUsrReceiverDao.getUsrReceiver(usrReceiverVO);
        if(temp != null) {
            usrReceiverVO.setIdCardFront("delete");
            usrReceiverVO.setId(temp.getId());
            return iUsrReceiverDao.updateUsrReceiver(usrReceiverVO);
        }
        return 0;
    }

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 删除收件人身份证反面信息
     */
    public int deleteUsrReceiverIdCardBack(UsrReceiverBO usrReceiver) {
        UsrReceiverVO usrReceiverVO = UsrReceiverBO.convert(usrReceiver);

        UsrReceiverVO temp = iUsrReceiverDao.getUsrReceiver(usrReceiverVO);
        if(temp != null) {
            usrReceiverVO.setIdCardBack("delete");
            usrReceiverVO.setId(temp.getId());
            return iUsrReceiverDao.updateUsrReceiver(usrReceiverVO);
        }
        return 0;
    }

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 删除收件人信息
     */
    public int deleteUsrReceiver(UsrReceiverBO usrReceiver) {
        UsrReceiverVO usrReceiverVO = UsrReceiverBO.convert(usrReceiver);
        return iUsrReceiverDao.deleteUsrReceiver(usrReceiverVO);
    }
}
